package com.lak.nagivation.cal;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lak.nagivation.bean.TXTResult;


import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TXT2Road {

    public static String[] ZXS = {"北京市", "上海市", "天津市", "重庆市"};
    public static List<String> ZXcity = Arrays.asList(ZXS);

    //城北收费站

    static String ori_text =
            "黑龙江省:奈门沁收费站-G10绥满高速-城北收费站-城北收费站-龙凤收费站-G45大广高速-黑吉界\n" +
                    "吉林省:吉黑界-G45大广高速-G12珲乌高速-G45大广高速—G25长深高速-吉蒙界\n" +
                    // "内蒙古自治区:蒙吉界-G25长深高速-金宝屯收费站-S305-甘旗卡收费站";
                    "内蒙古自治区:蒙吉界-G25长深高速-金宝屯收费站-S305-甘旗卡收费站-G2511新鲁高速-G45大广高速-蒙冀界\n" +
                    "河北省:冀蒙界-G45大广高速-冀京界-冀京界-G4京港澳高速-邯郸绕城高速-G4京港澳高速-冀豫界\n" +
                    "北京市:京冀界-G45大广高速-六环-G4京港澳高速-京冀界\n" +
                    "河南省:豫冀界-G4京港澳高速-S83兰南高速-G55二广高速-豫鄂界\n" +
                    "湖北省:鄂豫界-G55二广高速-G50沪渝高速-鄂渝界\n" +
                    "重庆市:渝鄂界-G50沪渝高速-桃花街互通-长涪高速-南涪高速-李渡收费站";
    static String ori_text1 ="新疆维吾尔自治区: 城北物流园--城北物流园--S303-哈密互通-G30连霍高速-烟墩收费站--G30连霍高速--星星峡收费站--新甘界\n" +
            "\n" +
            "甘肃省: 甘新界--柳园北收费站--G30连霍高速--G2201兰州南绕城高速--G30连霍高速--定西北站互通立交--交通路--S209--宁远镇--G310--G30连霍高速--东岔收费站--甘陕界\n" +
            "\n" +
            "陕西省: 陕甘界--陈仓收费站--G30连霍高速--G3021临兴高速--G30连霍高速--潼关收费站--陕豫界\n" +
            "\n" +
            "河南省: 豫陕界--G30连霍高速--G0321德上高速--豫皖界\n" +
            "\n" +
            "安徽省: 皖豫界--G1516盐洛高速--濉溪收费站-S238圣王路-百善-G343大卢线-五铺-G237济宁线--孟沟";
    /**
     * 将文本切割成地名组
     *
     * @param TXT 文本
     * @return java.util.ArrayList<com.alibaba.fastjson.JSONObject>
     * @author jiang
     * @creed: Good good study,day day up!
     * @date 2020/9/17 19:26
     */
    public ArrayList<JSONObject> PretreatmentTXT(String TXT) {
        ArrayList<JSONObject> groups = new ArrayList<JSONObject>();
        String pattern = "[\u4e00-\u9fa5]{2}界";
        Pattern r = Pattern.compile(pattern);

//        TXT = TXT.replaceAll("：", ":")
//                .replaceAll("—", "-");
        TXT = TXT.replaceAll("：", ":")
                .replaceAll(" ", "")
                .replaceAll("(\\n){2,}", "\n")
                .replaceAll("—", "-")
                .replaceAll("-{2,}", "-");
        String[] SplitProvince = TXT.split("\\n");
        for (int i = 0; i < SplitProvince.length; i++) {
            JSONObject group = new JSONObject();
            String[] RoadName;

            String[] split = SplitProvince[i].split(":");
            if (split.length > 1) {
                group.put("province", split[0]);
                RoadName = split[1].split("-");
            } else {
                group.put("province", "");
                RoadName = SplitProvince[i].split("-");
            }

            List<String> list = new ArrayList();
            for (int j = 0; j < RoadName.length; j++) {
                Matcher m = r.matcher(RoadName[j]);
                if (!list.contains(RoadName[j]) && !m.matches()) {
                    list.add(RoadName[j]);
                }
            }
            RoadName = list.toArray(new String[list.size()]);

            group.put("address", RoadName);
            groups.add(group);
        }
        System.out.println(groups);
        return groups;
    }


    public String getURL_Amap(String province, String address) throws UnsupportedEncodingException {
        address = URLEncoder.encode(address, "UTF-8");//先对中文进行编码
        String urlString = AmapServer.getHeadOfGeocode() + "address=" + address + "&output=JSON&key=" + AmapServer.getKEY();
        return getGeocode(urlString, province, address);

    }

    /**
     * 获取地名对应的坐标
     *
     * @param urlString 请求url
     * @param province  省份
     * @param address   地名
     * @return java.lang.String 坐标值
     * @author jiang
     * @creed: Good good study,day day up!
     * @date 2020/9/17 23:25
     */
    public static String getGeocode(String urlString, String province, String address) throws UnsupportedEncodingException {
        String location = "";
        String res = AmapServer.requestULR(urlString);
//        System.out.println(res);
        JSONObject jsonObject = JSON.parseObject(res);
        String count = jsonObject.getString("count");
        if (count.compareTo("0") != 0) {
            JSONArray geocodes = jsonObject.getJSONArray("geocodes");
            JSONObject geocode = geocodes.getJSONObject(0);
            String RE_province = geocode.getString("province");
            String level=geocode.getString("level");
            //获取数据库点位信息
            if (false) {
                return location;
            }

            //判定结果路段是否在指定省份
            if (RE_province.compareTo(province) == 0&&(level.compareTo("道路")==0||level.compareTo("兴趣点")==0)) {
                location = geocode.getString("location");

            } else {
                if (ZXcity.contains(province)) {
                    urlString = AmapServer.getHeadOfGeocode() + "address=" + address + "&" + "city=" + URLEncoder.encode(province, "UTF-8") + "&output=JSON&key=" + AmapServer.getKEY();
                    return getGeocode(urlString, province, address);
                } else {
                    urlString = AmapServer.getHeadOfSearchPOI() + "keywords=" + address + "&types=190300" + "&city=" + URLEncoder.encode(province, "UTF-8") + "&output=JSON&key=" + AmapServer.getKEY();
                    return getSearchPOI(urlString,province,address);
                }

            }
        } else {
            urlString = AmapServer.getHeadOfSearchPOI() + "keywords=" + address + "&types=190300" + "&city=" + URLEncoder.encode(province, "UTF-8") + "&output=JSON&key=" + AmapServer.getKEY();
            return getSearchPOI(urlString,province,address);
        }

        return location;
    }

    public static String getSearchPOI(String urlString, String province, String address) throws UnsupportedEncodingException {
        String location = "";
        String res = AmapServer.requestULR(urlString);
        JSONObject jsonObject = JSON.parseObject(res);
        String count = jsonObject.getString("count");
        JSONObject sug_address = jsonObject.getJSONObject("sug_address");
        if (sug_address!=null){
            location=sug_address.getString("location");
        }
        else if (count.compareTo("0") != 0) {
            JSONArray pois = jsonObject.getJSONArray("pois");
            JSONObject poi = pois.getJSONObject(0);  //暂时默认第一个值
            location=poi.getString("location");
        }else {
            location="null";
        }

        return location;
    }


    public static TXTResult getGeocode(String text) throws UnsupportedEncodingException {
        TXT2Road txt = new TXT2Road();
        ArrayList<JSONObject> groups = txt.PretreatmentTXT(text);
        String p;
        String[] addresses;
        JSONObject group;

        JSONArray jsonArray=new JSONArray();
        for (int i = 0; i < groups.size(); i++) {
            JSONObject re_group = new JSONObject();
            group = groups.get(i);
            p = group.getString("province");
            addresses = (String[]) group.get("address");

            re_group.put("province",p);
            JSONArray Jarray=new JSONArray();
            for (int j = 0; j < addresses.length; j++) {
                String address = addresses[j];
                String geocode=txt.getURL_Amap(p, address);

                JSONObject jo = new JSONObject();
                jo.put("address",address);
                jo.put("geocode",geocode);
                Jarray.add(jo);
                System.out.println(address+":"+geocode);
            }
            re_group.put("geocodes",Jarray);
            jsonArray.add(re_group);
        }

        TXTResult txtResult=new TXTResult();
        txtResult.setRoadcode(jsonArray);
//        return  "sf";
        return txtResult;

    }

    public static void main(String[] args) throws UnsupportedEncodingException {
        getGeocode(ori_text1);
//       String address="城北收费站";
//       String province="黑龙江省";
//       address = URLEncoder.encode(address, "UTF-8");//先对中文进行编码
//       String U1=AmapServer.getHeadOfGeocode() + "address=" + address + "&output=JSON&key=" + AmapServer.getKEY();
//       String  UUU=AmapServer.getHeadOfGeocode() + "address=" +URLEncoder.encode(address, "UTF-8") + "&" + "city=" + URLEncoder.encode(province, "UTF-8") + "&output=JSON&key=" + AmapServer.getKEY();
////       String urlString = AmapServer.getHeadOfSearchPOI() + "keywords=" + URLEncoder.encode(address, "UTF-8") + "&types=190300" + "&city=" + URLEncoder.encode(province, "UTF-8") + "&output=JSON&key=" + AmapServer.getKEY();
//        String urlString="https://restapi.amap.com/v3/place/text?"+ "keywords=" + address +"&city=" + URLEncoder.encode(province, "UTF-8")+ "&output=JSON&key="  + AmapServer.getKEY();
//
//       System.out.println(getSearchPOI(urlString,province,address));

    }
//    static String ori_text =
//            "黑龙江省:奈门沁收费站-G10绥满高速-城北收费站-城北收费站-龙凤收费站-G45大广高速-黑吉界\n" +
//                    "吉林省:吉黑界-G45大广高速-G12珲乌高速-G45大广高速—G25长深高速-吉蒙界\n" +
//                    // "内蒙古自治区:蒙吉界-G25长深高速-金宝屯收费站-S305-甘旗卡收费站";
//                    "内蒙古自治区:蒙吉界-G25长深高速-金宝屯收费站-S305-甘旗卡收费站-G2511新鲁高速-G45大广高速-蒙冀界\n" +
//                    "河北省:冀蒙界-G45大广高速-冀京界-冀京界-G4京港澳高速-邯郸绕城高速-G4京港澳高速-冀豫界\n" +
//                    "北京市:京冀界-G45大广高速-六环-G4京港澳高速-京冀界\n" +
//                    "河南省:豫冀界-G4京港澳高速-S83兰南高速-G55二广高速-豫鄂界\n" +
//                    "湖北省:鄂豫界-G55二广高速-G50沪渝高速-鄂渝界\n" +
//                    "重庆市:渝鄂界-G50沪渝高速-桃花街互通-长涪高速-南涪高速-李渡收费站";

}
